﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using grupo2DEWStudio7i.Negocio;
using grupo2DEWStudio7i.Dominio;
using grupo2DEWStudio7i.Persistencia;

namespace grupo2DEWStudio7i.Controllers
{
    public class InstrumentoController : Controller
    {
        //Dependencias
        public AdministracionServiceImpl AdminService{get;set;}
        // GET: /Instrumento/

        public ActionResult Index()
        {
            ICollection<Instrumento> lista =  AdminService.ListarInstrumento();

            return View(lista);
        }

        //
        // GET: /Instrumento/Details/5

        public ActionResult Details(int cod)
        {
            Instrumento modelo = AdminService.ObtenerInstrumento(cod);
            return View(modelo);
        }

        //
        // GET: /Instrumento/Create
        public ActionResult Create()
        {
            try
            {
                return View();
            }
            catch
            {
                ModelState.AddModelError(String.Empty, "Error al crear Instrumento");
                return View();
            }
        }

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            Instrumento instrumentoaCrear = new Instrumento();
            {
            //instrumentoaCrear.CodInstrumento = int.Parse(collection["codigo"]);
            instrumentoaCrear.TipoInstru = (string)collection["TipoInstru"];
            instrumentoaCrear.ModeloInstru = (string)collection["ModeloInstru"];
            instrumentoaCrear.MarcaInstru = (string)collection["MarcaInstru"];
            instrumentoaCrear.AñoFabricacion = int.Parse(collection["AñoFabricacion"]);
            instrumentoaCrear.Caracteristicas = (string)collection["Caracteristicas"];
            instrumentoaCrear.CostoxHora = decimal.Parse(collection["CostoxHora"]);
            };
            try
            {
                //if (ModelState.IsValid)
                //{
                    //Instrumento instrumentoaCrear = new Instrumento();
                    ////instrumentoaCrear.CodInstrumento = int.Parse(collection["codigo"]);
                    //instrumentoaCrear.TipoInstru = collection["TipoInstru"];
                    //instrumentoaCrear.ModeloInstru = collection["ModeloInstru"];
                    //instrumentoaCrear.MarcaInstru = collection["MarcaInstru"];
                    //instrumentoaCrear.AñoFabricacion = int.Parse(collection["AñoFabricacion"]);
                    //instrumentoaCrear.Caracteristicas = collection["Caracteristicas"];
                    //instrumentoaCrear.CostoxHora = decimal.Parse(collection["CostoxHora"]);
                    //AdminService.CrearInstrumento(instrumentoaCrear);
                    Instrumento instrumentoCreado = AdminService.CrearInstrumento(instrumentoaCrear);
                    TempData["mensaje"] = "Instrumento " + instrumentoCreado.CodInstrumento + " creado exitosamente";
                //}
                return RedirectToAction("Index");
            }
            catch(Exception ex)
            {
                ModelState.AddModelError(String.Empty, "Error al crear Instrumento: " + ex.Message);
                return View("Create", instrumentoaCrear);
            }
        }
        
        //
        // GET: /Instrumento/Edit/5
 
        public ActionResult Edit(int id)
        {
            Instrumento instrumentoaCrear = AdminService.ObtenerInstrumento(id);

            return View(instrumentoaCrear);
        }

        //
        // POST: /Instrumento/Edit/5

        [HttpPost]
        public ActionResult Edit(FormCollection collection)
        {
            Instrumento instrumentoaModificar = new Instrumento();
            {
                instrumentoaModificar.CodInstrumento = int.Parse(collection["CodInstrumento"]);
                instrumentoaModificar.TipoInstru = (string)collection["TipoInstru"];
                instrumentoaModificar.ModeloInstru = (string)collection["ModeloInstru"];
                instrumentoaModificar.MarcaInstru = (string)collection["MarcaInstru"];
                instrumentoaModificar.AñoFabricacion = int.Parse(collection["AñoFabricacion"]);
                instrumentoaModificar.Caracteristicas = (string)collection["Caracteristicas"];
                instrumentoaModificar.CostoxHora = decimal.Parse(collection["CostoxHora"]);
            };
            try
            {
                //if (ModelState.IsValid)
                //{
                    //Instrumento instrumentoaModificar = new Instrumento();
                    ////instrumentoaCrear.CodInstrumento = int.Parse(collection["codigo"]);
                    //instrumentoaModificar.TipoInstru = collection["TipoInstru"];
                    //instrumentoaModificar.ModeloInstru = collection["ModeloInstru"];
                    //instrumentoaModificar.MarcaInstru = collection["MarcaInstru"];
                    //instrumentoaModificar.AñoFabricacion = int.Parse(collection["AñoFabricacion"]);
                    //instrumentoaModificar.Caracteristicas = collection["Caracteristicas"];
                    //instrumentoaModificar.CostoxHora = decimal.Parse(collection["CostoxHora"]);
                    //AdminService.CrearInstrumento(instrumentoaCrear);
                    Instrumento instrumentoModificado = AdminService.ModificarInstrumento(instrumentoaModificar);
                    TempData["mensaje"] = "Instrumento " + instrumentoModificado.CodInstrumento + " modificado exitosamente";
                //}
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(String.Empty, "Error al crear Instrumento: " + ex.Message);
                return View("Edit", instrumentoaModificar);
            }
        }

        //
        // GET: /Instrumento/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(AdminService.ObtenerInstrumento(id));
        }

        //
        // POST: /Instrumento/Delete/5

        [HttpPost]
        public ActionResult Eliminar(int id)
        {
            try
            {
                Instrumento instrumentoAEliminar = AdminService.ObtenerInstrumento(id);
                AdminService.EliminarInstrumento(instrumentoAEliminar);
                TempData["mensaje"] = "Instrumento " + instrumentoAEliminar.CodInstrumento + " eliminado exitosamente";
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {

                ModelState.AddModelError(string.Empty, ex.Message);
                return View("Delete", AdminService.ObtenerInstrumento(id));
            }
        }
    }
}
